﻿using System.Collections.Generic;
using System.Linq;
using QFramework;
using Unity.VisualScripting;
using NotImplementedException = System.NotImplementedException;

namespace ProjectSurvival
{
    public class LevelUpSystem : AbstractSystem
    {
        public List<LevelUpItem> LevelUpItems { get; } = new();

        public static EasyEvent OnUpgradeStateChanged = new();

        public static bool AllUnlockedFinish = false;

        public static void CheckAllUnlockedFInish()
        {
            AllUnlockedFinish =Global.Interface.GetSystem<LevelUpSystem>().LevelUpItems.All(item => item.UpgradeFinish);
        }

        //设置配对技能
        public Dictionary<string, string> Pairs = new Dictionary<string, string>()
        {
            {"simple_Sword","Critical_Rate"},
            {"simple_Bomb","FlyThing_Count"},
            {"simple_knife","Damage_Rate"},
            {"simple_BasketBall","Movement_SpeedRate"},
            {"simple_Rotate_Sword","Additional_ExpPercent"},
            
            {"Critical_Rate","simple_Sword"},
            {"FlyThing_Count","simple_Bomb"},
            {"Damage_Rate","simple_knife"},
            {"Movement_SpeedRate","simple_BasketBall"},
            {"Additional_ExpPercent","simple_Rotate_Sword"}
        };

        //是否解锁超级武器
        public Dictionary<string, BindableProperty<bool>> PairedProperties =
            new Dictionary<string, BindableProperty<bool>>()
            {
                {"simple_Sword",Global.SuperSword},
                {"simple_Bomb",Global.SuperBomb},
                {"simple_knife",Global.SuperKnife},
                {"simple_BasketBall",Global.SuperBasketBall},
                {"simple_Rotate_Sword",Global.SuperRotateSword}
            };

        //将levelUpItems转为字典,用item.key做字典的key
        public Dictionary<string, LevelUpItem> Dictionary = new Dictionary<string, LevelUpItem>();
        
        public void ResetData()
        {
            LevelUpItems.Clear();

            #region 升级系统设置
            //普通剑升级系统
            AddItem(new LevelUpItem(true)
                .WithKey("simple_Sword")
                .WithName("简单剑")
                .WithIconName("simple_sword_icon")
                .WithPairedName("超级剑")
                .WithPairedDescription("攻击力翻倍，范围翻倍")
                .WithPairedIconName("paired_simple_sword_icon")
                .WithMaxLevel(10)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"剑Lv{lv}：攻击身边的敌人",
                        2 => $"剑Lv{lv}:\n攻击力+3数量+2",
                        3 => $"剑Lv{lv}:\n攻击力+2间隔-0.25s",
                        4 => $"剑Lv{lv}:\n攻击力+2间隔-0.25s",
                        5 => $"剑Lv{lv}：\n攻击力+3数量+2",
                        6 => $"剑Lv{lv}:\n范围+1间隔-0.25s",
                        7 => $"剑Lv{lv}:\n攻击力+3数量+2",
                        8 => $"剑Lv{lv}:\n攻击力+2范围+1",
                        9 => $"剑Lv{lv}:\n攻击力+3间隔-0.25",
                        10 => $"剑Lv{lv}:\n攻击力+3数量+2",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.SimpleSwordUnlocked.Value = true;
                            break;
                        case 2:
                            Global.AbilityDamage.Value += 3;
                            Global.SimpleSwordCount.Value += 2;
                            break;
                        case 3:
                            Global.AbilityDamage.Value += 2;
                            Global.AttackDuration.Value -= 0.25f;
                            break;
                        case 4:
                            Global.AbilityDamage.Value += 2;
                            Global.AttackDuration.Value -= 0.25f;
                            break;
                        case 5:
                            Global.AbilityDamage.Value += 3;
                            Global.SimpleSwordCount.Value += 2;
                            break;
                        case 6:
                            Global.SimpleSwordRange.Value += 1f;
                            Global.AttackDuration.Value -= 0.25f;
                            break;
                        case 7:
                            Global.AbilityDamage.Value += 3;
                            Global.SimpleSwordCount.Value += 2;
                            break;
                        case 8:
                            Global.SimpleSwordRange.Value += 1f;
                            Global.AbilityDamage.Value += 2;
                            break;
                        case 9:
                            Global.AbilityDamage.Value += 3;
                            Global.AttackDuration.Value -= 0.25f;
                            break;
                        case 10:
                            Global.AbilityDamage.Value += 3;
                            Global.SimpleSwordCount.Value += 2;
                            break;
                    }
                })
            );

            //飞刀升级系统
            AddItem(new LevelUpItem(true)
                .WithKey("simple_knife")
                .WithName("飞刀")
                .WithIconName("simple_knife_icon")
                .WithPairedName("超级飞刀")
                .WithPairedDescription("攻击力翻倍，可穿透敌人")
                .WithPairedIconName("paired_simple_knife_icon")
                .WithMaxLevel(10)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"飞刀Lv{lv}：\n向最近的敌人发射一把飞刀",
                        2 => $"飞刀Lv{lv}:\n攻击力+3 数量+2",
                        3 => $"飞刀Lv{lv}:\n攻击力+1 数量+1 间隔-0.1s",
                        4 => $"飞刀Lv{lv}:\n穿透+1 数量+1 间隔-0.1s",
                        5 => $"飞刀Lv{lv}：\n攻击力+3 数量+1",
                        6 => $"飞刀Lv{lv}:\n数量+1 间隔-0.1s",
                        7 => $"飞刀Lv{lv}:\n数量+1 穿透+1 间隔-0.1",
                        8 => $"飞刀Lv{lv}:\n攻击力+3 数量+1",
                        9 => $"飞刀Lv{lv}:\n数量+1 间隔-0.1",
                        10 => $"飞刀Lv{lv}:\n攻击力+3 数量+1",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.SimpleKnifeUnlocked.Value = true;
                            break;
                        case 2:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeCount.Value += 2;
                            break;
                        case 3:
                            Global.SimpleKnifeDamage.Value += 1;
                            Global.SimpleKnifeCount.Value += 1;
                            Global.SimpleKnifeDuration.Value -= 0.1f;
                            break;
                        case 4:
                            Global.SimpleKnifeCount.Value += 1;
                            Global.SimpleKnifeDuration.Value -= 0.1f;
                            Global.SimpleKnifeAttackCount.Value++;
                            break;
                        case 5:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeCount.Value += 1;
                            break;
                        case 6:
                            Global.SimpleKnifeCount.Value += 1;
                            Global.SimpleKnifeDuration.Value -= 0.1f;
                            break;
                        case 7:
                            Global.SimpleKnifeCount.Value += 1;
                            Global.SimpleKnifeDuration.Value -= 0.1f;
                            Global.SimpleKnifeAttackCount.Value++;
                            break;
                        case 8:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeCount.Value += 1;
                            break;
                        case 9:
                            Global.SimpleKnifeCount.Value += 1;
                            Global.SimpleKnifeDuration.Value -= 0.1f;
                            break;
                        case 10:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeCount.Value += 1;
                            break;
                    }
                })
            );
            
            //守卫剑升级系统
            AddItem(new LevelUpItem(true)
                .WithKey("simple_Rotate_Sword")
                .WithName("守卫剑")
                .WithIconName("rotate_sword_icon")
                .WithPairedName("超级守卫剑")
                .WithPairedDescription("攻击力翻倍，转速翻倍")
                .WithPairedIconName("paired_rotate_sword_icon")
                .WithMaxLevel(10)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"守卫剑Lv{lv}：\n环绕主角身边的剑",
                        2 => $"守卫剑Lv{lv}:\n数量+1 攻击力+1",
                        3 => $"守卫剑Lv{lv}:\n攻击力+2 速度+25%",
                        4 => $"守卫剑Lv{lv}:\n速度+50%",
                        5 => $"守卫剑Lv{lv}：\n数量+1 攻击力+1",
                        6 => $"守卫剑Lv{lv}:\n攻击力+2 速度+25%",
                        7 => $"守卫剑Lv{lv}:\n数量+1 攻击力+1",
                        8 => $"守卫剑Lv{lv}:\n攻击力+2 速度+25%",
                        9 => $"守卫剑Lv{lv}:\n数量+1 攻击力+1",
                        10 => $"守卫剑Lv{lv}:\n攻击力+2 速度+25%",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.SimpleRotateSworUnlocked.Value = true;
                            break;
                        case 2:
                            Global.SimpleRotateSwordCount.Value += 1;
                            Global.SimpleRotateSwordDamage.Value += 1;
                            break;
                        case 3:
                            Global.SimpleRotateSwordDamage.Value += 2;
                            Global.SimpleRotateSwordSpeed.Value *= 1.25f;
                            break;
                        case 4:
                            Global.SimpleRotateSwordSpeed.Value *= 1.5f;
                            break;
                        case 5:
                            Global.SimpleRotateSwordCount.Value += 1;
                            Global.SimpleRotateSwordDamage.Value += 1;
                            break;
                        case 6:
                            Global.SimpleRotateSwordDamage.Value += 2;
                            Global.SimpleRotateSwordSpeed.Value *= 1.25f;
                            break;
                        case 7:
                            Global.SimpleRotateSwordCount.Value += 1;
                            Global.SimpleRotateSwordDamage.Value += 1;
                            break;
                        case 8:
                            Global.SimpleRotateSwordDamage.Value += 2;
                            Global.SimpleRotateSwordSpeed.Value *= 1.25f;
                            break;
                        case 9:
                            Global.SimpleRotateSwordCount.Value += 1;
                            Global.SimpleRotateSwordDamage.Value += 1;
                            break;
                        case 10:
                            Global.SimpleRotateSwordDamage.Value += 2;
                            Global.SimpleRotateSwordSpeed.Value *= 1.25f;
                            break;
                    }
                })
            );
            
            //篮球升级系统
            AddItem(new LevelUpItem(true)
                .WithKey("simple_BasketBall")
                .WithName("篮球")
                .WithIconName("ball_icon")
                .WithPairedName("超级篮球")
                .WithPairedDescription("速度翻倍，伤害翻倍，体积翻倍")
                .WithPairedIconName("paired_ball_icon")
                .WithMaxLevel(10)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"篮球Lv{lv}：\n在屏幕内反弹的篮球",
                        2 => $"篮球Lv{lv}:\n攻击力+3",
                        3 => $"篮球Lv{lv}:\n数量+1",
                        4 => $"篮球Lv{lv}:\n攻击力+3",
                        5 => $"篮球Lv{lv}：\n数量+1",
                        6 => $"篮球Lv{lv}:\n攻击力+3",
                        7 => $"篮球Lv{lv}:\n速度+20%",
                        8 => $"篮球Lv{lv}:\n攻击力+3",
                        9 => $"篮球Lv{lv}:\n速度+20%",
                        10 => $"篮球Lv{lv}:\n数量+1",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.SimpleBasketBallUnlocked.Value = true;
                            break;
                        case 2:
                            Global.SimpleBasketBallDamage.Value += 3;
                            break;
                        case 3:
                            Global.SimpleBasketBallCount.Value += 1;
                            break;
                        case 4:
                            Global.SimpleBasketBallDamage.Value += 3;
                            break;
                        case 5:
                            Global.SimpleBasketBallCount.Value += 1;
                            break;
                        case 6:
                            Global.SimpleBasketBallDamage.Value += 3;
                            break;
                        case 7:
                            Global.SimpleBasketBallSpeed.Value *= 1.2f;
                            break;
                        case 8:
                            Global.SimpleBasketBallDamage.Value += 3;
                            break;
                        case 9:
                            Global.SimpleBasketBallSpeed.Value *= 1.2f;
                            break;
                        case 10:
                            Global.SimpleBasketBallCount.Value += 1;
                            break;
                    }
                })
            );
            
            //炸弹升级系统
            AddItem(new LevelUpItem(false)
                .WithKey("simple_Bomb")
                .WithName("炸弹")
                .WithIconName("bomb_icon")
                .WithPairedName("超级炸弹")
                .WithPairedDescription("每15秒爆炸一次")
                .WithPairedIconName("paired_bomb_icon")
                .WithMaxLevel(10)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"炸弹Lv{lv}：\n伤害全屏的敌人",
                        2 => $"炸弹Lv{lv}:\n伤害+5 掉率+0.05",
                        3 => $"炸弹Lv{lv}:\n伤害+5 掉率+0.05",
                        4 => $"炸弹Lv{lv}:\n伤害+5 掉率+0.05",
                        5 => $"炸弹Lv{lv}：\n伤害+5 掉率+0.05",
                        6 => $"炸弹Lv{lv}:\n伤害+5 掉率+0.05",
                        7 => $"炸弹Lv{lv}:\n伤害+5 掉率+0.05",
                        8 => $"炸弹Lv{lv}:\n伤害+5 掉率+0.05",
                        9 => $"炸弹Lv{lv}:\n伤害+5 掉率+0.05",
                        10 => $"炸弹Lv{lv}:\n伤害+5 掉率+0.05",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.SimpleBombUnlocked.Value = true;
                            break;
                        case 2:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                        case 3:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                        case 4:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                        case 5:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                        case 6:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                        case 7:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                        case 8:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                        case 9:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                        case 10:
                            Global.SimpleBombDamage.Value += 5;
                            Global.SimpleBombPercent.Value += 0.05f;
                            break;
                    }
                })
            );
            
            //暴击率升级
            AddItem(new LevelUpItem(false)
                .WithKey("Critical_Rate")
                .WithName("暴击率")
                .WithIconName("critical_icon")
                .WithMaxLevel(5)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"暴击Lv{lv}：\n15%暴击率",
                        2 => $"暴击Lv{lv}:\n28%暴击率",
                        3 => $"暴击Lv{lv}:\n43%暴击率",
                        4 => $"暴击Lv{lv}:\n65%暴击率",
                        5 => $"暴击Lv{lv}：\n80%暴击率",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.CriticalRate.Value = 0.15f;
                            break;
                        case 2:
                            Global.CriticalRate.Value = 0.28f;
                            break;
                        case 3:
                            Global.CriticalRate.Value = 0.43f;
                            break;
                        case 4:
                            Global.CriticalRate.Value = 0.65f;
                            break;
                        case 5:
                            Global.CriticalRate.Value = 0.80f;
                            break;
                    }
                })
            );
            
            //伤害率升级
            AddItem(new LevelUpItem(false)
                .WithKey("Damage_Rate")
                .WithName("伤害倍率")
                .WithIconName("damage_icon")
                .WithMaxLevel(5)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"伤害Lv{lv}：\n120%伤害率",
                        2 => $"伤害Lv{lv}:\n140%伤害率",
                        3 => $"伤害Lv{lv}:\n160%伤害率",
                        4 => $"伤害Lv{lv}:\n180%伤害率",
                        5 => $"伤害Lv{lv}：\n200%伤害率",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.DamageRate.Value += 1.2f;
                            break;
                        case 2:
                            Global.DamageRate.Value += 1.4f;
                            break;
                        case 3:
                            Global.DamageRate.Value += 1.6f;
                            break;
                        case 4:
                            Global.DamageRate.Value += 1.8f;
                            break;
                        case 5:
                            Global.DamageRate.Value += 2.0f;
                            break;
                    }
                })
            );
            //伤害率升级
            AddItem(new LevelUpItem(false)
                .WithKey("FlyThing_Count")
                .WithName("飞行物数量")
                .WithIconName("fly_icon")
                .WithMaxLevel(3)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"飞行物数量Lv{lv}：\n额外增加1个",
                        2 => $"飞行物数量Lv{lv}:\n额外增加2个",
                        3 => $"飞行物数量Lv{lv}:\n额外增加3个",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.AdditionalFlyThingCount.Value = 1;
                            break;
                        case 2:
                            Global.AdditionalFlyThingCount.Value = 2;
                            break;
                        case 3:
                            Global.AdditionalFlyThingCount.Value = 3;
                            break;
                    }
                })
            );
            
             
            //移动速度升级
            AddItem(new LevelUpItem(false)
                .WithKey("Movement_SpeedRate")
                .WithName("移动速率")
                .WithIconName("movement_icon")
                .WithMaxLevel(5)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"移动速率Lv{lv}：\n增加25%移速",
                        2 => $"移动速率Lv{lv}:\n增加50%移速",
                        3 => $"移动速率Lv{lv}:\n增加75%移速",
                        4 => $"移动速率Lv{lv}:\n增加100%移速",
                        5 => $"移动速率Lv{lv}：\n增加150%移速",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.MovementSpeedRate.Value = 1.25f;
                            break;
                        case 2:
                            Global.MovementSpeedRate.Value = 1.5f;
                            break;
                        case 3:
                            Global.MovementSpeedRate.Value = 1.75f;
                            break;
                        case 4:
                            Global.MovementSpeedRate.Value = 2.0f;
                            break;
                        case 5:
                            Global.MovementSpeedRate.Value += 2.5f;
                            break;
                    }
                })
            );
            
            //拾取范围升级
            AddItem(new LevelUpItem(false)
                .WithKey("Collectable_Area")
                .WithName("拾取范围")
                .WithIconName("collectable_icon")
                .WithMaxLevel(3)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"拾取范围Lv{lv}：\n增加100%",
                        2 => $"拾取范围Lv{lv}:\n增加200%",
                        3 => $"拾取范围Lv{lv}:\n增加300%",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.CollectableArea.Value = 2;
                            break;
                        case 2:
                            Global.CollectableArea.Value = 3;
                            break;
                        case 3:
                            Global.CollectableArea.Value = 4;
                            break;
                    }
                })
            );
            //拾取范围提升
            AddItem(new LevelUpItem(false)
                .WithKey("Additional_ExpPercent")
                .WithName("经验值掉落概率")
                .WithIconName("exp_icon")
                .WithMaxLevel(5)
                .WithDescription((lv) =>
                {
                    return lv switch
                    {
                        1 => $"经验值Lv{lv}:\n额外增加5%掉落概率",
                        2 => $"经验值Lv{lv}:\n额外增加8%掉落概率",
                        3 => $"经验值Lv{lv}:\n额外增加12%掉落概率",
                        4 => $"经验值Lv{lv}:\n额外增加17%掉落概率",
                        5 => $"经验值Lv{lv}:\n额外增加25%掉落概率",
                        _ => null
                    };
                })
                .OnUpgrade((_, lv) =>
                {
                    switch (lv)
                    {
                        case 1:
                            Global.AdditionalExpPercent.Value = 0.05f;
                            break;
                        case 2:
                            Global.AdditionalExpPercent.Value = 0.08f;
                            break;
                        case 3:
                            Global.AdditionalExpPercent.Value = 0.12f;
                            break;
                        case 4:
                            Global.AdditionalExpPercent.Value = 0.17f;
                            break;
                        case 5:
                            Global.AdditionalExpPercent.Value = 0.25f;
                            break;
                    }
                }));
            #endregion
            Dictionary = LevelUpItems.ToDictionary(i => i.Key,i=>i);
        }

        public LevelUpItem AddItem(LevelUpItem item)
        {
            LevelUpItems.Add(item);
            return item;
        }

        protected override void OnInit()
        {
            ResetData();
            Global.Level.Register(level => Roll());
        }

        private void Roll()
        {
            foreach (var levelUpItem in LevelUpItems)
            {
                levelUpItem.Visible.Value = false;
            }

            var list = LevelUpItems.Where(item => !item.UpgradeFinish).ToList();
            if (list.Count>=4)
            {
                list.GetAndRemoveRandomItem().Visible.Value = true;
                list.GetAndRemoveRandomItem().Visible.Value = true;
                list.GetAndRemoveRandomItem().Visible.Value = true;
                list.GetAndRemoveRandomItem().Visible.Value = true;
            }
            else
            {
                foreach (var item in list)
                {
                    item.Visible.Value = true;
                }
            }
        }
    }
}